package com.yayun.merchant.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.yayun.common.annotation.Log;
import com.yayun.common.core.controller.BaseController;
import com.yayun.common.core.domain.AjaxResult;
import com.yayun.common.enums.BusinessType;
import com.yayun.merchant.domain.OmsCart;
import com.yayun.merchant.service.IOmsCartService;

/**
 * 购物车管理Controller
 */
@RestController
@RequestMapping("/cart")
public class OmsCartController extends BaseController {
    @Autowired
    private IOmsCartService cartService;

    /**
     * 获取购物车列表
     */
    @GetMapping("/list")
    public AjaxResult list() {
        List<OmsCart> list = cartService.selectCartList(getUserId());
        return success(list);
    }

    /**
     * 添加商品到购物车
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody OmsCart cart) {
        // 参数校验
        if (cart.getProductId() == null) {
            return error("商品ID不能为空");
        }
        if (cart.getQuantity() == null || cart.getQuantity() < 1) {
            return error("商品数量不正确");
        }
        if (cart.getPrice() == null) {
            return error("商品价格不能为空");
        }
        if (cart.getProductName() == null) {
            return error("商品名称不能为空");
        }

        cart.setUserId(getUserId());
        return toAjax(cartService.addCart(cart));
    }

    /**
     * 修改购物车商品数量
     */
    @PutMapping("/update/quantity")
    public AjaxResult updateQuantity(@RequestBody OmsCart cart) {
        cart.setUserId(getUserId());
        return toAjax(cartService.updateQuantity(cart));
    }

    /**
     * 修改购物车商品选中状态
     */
    @PutMapping("/update/checked")
    public AjaxResult updateChecked(@RequestBody OmsCart cart) {
        cart.setUserId(getUserId());
        return toAjax(cartService.updateChecked(cart));
    }

    /**
     * 删除购物车商品
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(cartService.deleteCart(ids));
    }

    /**
     * 清空购物车
     */
    @DeleteMapping("/clear")
    public AjaxResult clear() {
        return toAjax(cartService.clearCart(getUserId()));
    }
} 